package com.city.project.dao.impl;

import com.city.project.dao.IRoomDao;
import com.city.project.factory.ConnectionFactory;
import com.city.project.model.Room;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomDaoImpl implements IRoomDao {
    private Room createRoom(ResultSet rs) throws SQLException {
        Room room = new Room();
        room.setCell(rs.getString("cell"));
        room.setFloor(rs.getString("floor"));
        room.setNumber(rs.getString("number"));
        return room;
    }
    @Override
    public List<Room> getList(String number) {
        Connection connection = ConnectionFactory.getConnection();
        List<Room> list = new ArrayList<>();
        String sql = "select * from room";
        if (number == null || "".equals(number.trim())) {
            sql = "select * from room where number like '%" + number + "%'";
        }
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(createRoom(resultSet));
            }
            connection.close();
            preparedStatement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public Room getOne(int id) {
        Connection connection = ConnectionFactory.getConnection();
        List<Room> list = new ArrayList<>();
        String sql = "select * from room where id="+id;
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(createRoom(resultSet));
            }
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list.get(0);
    }

    @Override
    public void add(Room room) {
        Connection connection = ConnectionFactory.getConnection();
        String sql = "insert into room(number,cell,floor) values(?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, room.getNumber());
            preparedStatement.setString(2, room.getCell());
            preparedStatement.setString(3, room.getFloor());
            preparedStatement.executeUpdate();
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(Room room) {
        Connection connection = ConnectionFactory.getConnection();
        String sql = "update room set number=?,cell=?,floor=? where id=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, room.getNumber());
            preparedStatement.setString(2, room.getCell());
            preparedStatement.setString(3, room.getFloor());
            preparedStatement.setInt(4, room.getId());
            preparedStatement.executeUpdate();
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(int id) {
        Connection connection = ConnectionFactory.getConnection();
        String sql = "delete from room where id=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
